package com.shop.bookstore.service;

import com.shop.bookstore.dao.BorrowDao;
import com.shop.bookstore.entity.Borrow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BorrowService implements BorrowDao {
    //    实现操作方法增删改查
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int add(Borrow borrow) {
        String sql = "insert into borrow values(null,?,?,?,?)";
//        调用方法实现
        Object[] args = {borrow.getBookname(),borrow.getBorrow_time(),borrow.getBorrow_state(),borrow.getBack_time()};
        return jdbcTemplate.update(sql, args);

    }

    @Override
    public int delete(Borrow borrow) {
        String sql = "delete from borrow where Id=?";
        Object[] args={borrow.getId()};
        return jdbcTemplate.update(sql, args);
    }

    /**
     * 更新商品操作
     * @param borrow
     * @return
     */

    @Override
    public int update(Borrow borrow) {
        String sql = "update borrow set bookname=?,borrow_time=?,borrow_state=?,back_time=? where Id=?";
        Object[] args={borrow.getBookname(),borrow.getBorrow_time(),borrow.getBorrow_state(),borrow.getBack_time(),
                borrow.getId()};
        return jdbcTemplate.update(sql, args);
    }

    /***
     *
     * @param borrow
     * @return
     */
    @Override
    public Borrow findOne(Borrow borrow) {
        String sql = "select * from borrow where Id="+borrow.getId().toString();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Borrow>(Borrow.class));
    }

    /**
     * 获取所有商品列表的方法
     * @return
     */
    @Override
    public List<Borrow> findAll() {
        String sql = "select * from borrow";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Borrow>(Borrow.class));
    }

}
